package no.ntnu.fp.model;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import query.KalenderPunkt;

import no.ntnu.fp.swingutil.CalendarTools;

public class CalendarModel {
	private ArrayList<ActivityModel> activities = new ArrayList<ActivityModel>();
	private Person owner;
	private CalendarTools ct;
	
	/**
	 * @param owner of the calendar
	 */
	public CalendarModel(Person owner){
		this.owner = owner;
		ct = new CalendarTools();
		updateAppointments();
	}
	
	/**
	 * A method that gets the database to dig out my activities
	 * notTODO: Write a variant that inserts a specific KID into activities
	 */
	public void updateAppointments() {
		activities = KalenderPunkt.getActivities(owner);
	}
	
	/**
	 * 
	 * @param The newly made appointment.
	 * @return the KID the appointment has in the DB.
	 * @throws SQLException
	 */
	public int insertActivity(ActivityModel avtale) throws SQLException {
		int newAvtaleID = KalenderPunkt.insertAvtale(avtale);
		updateAppointments();
		return newAvtaleID;
	}
	
	/**
	 * Method for getting the Person's schedule for a day
	 * @param date indicating which day to get
	 * @return ArrayList of ActivityModels for that day.
	 */
	public ArrayList<ActivityModel> getScheduleDay(Date date){
		ArrayList<ActivityModel> retur = new ArrayList<ActivityModel>();
		for (int i = 0; i < activities.size(); i++) {
			if (ct.isSameDate(activities.get(i).getStartDate(), date, true) ||
								ct.isSameDate(activities.get(i).getEndDate(), date, true)) {
				retur.add(activities.get(i));
			}
		}
		return retur;
	}
	
	/**
	 * Method for getting the Person's schedule for a week 
	 * @param week indicating which week to get
	 * @return ArrayList of ActivityModels for that day.
	 */
	public ArrayList<ActivityModel> getScheduleWeek(Date week){
		week = ct.setDayOfWeek(week, 0);
		ArrayList<ActivityModel> retur = new ArrayList<ActivityModel>();
		for (int i = 0; i < 7; i++) {
			retur.addAll(getScheduleDay(week));
			week = ct.getNextDay(week);
		}
		return retur;
	}
	
	/**
	 * Method for getting the Person's schedule for a Month
	 * @param month indicating which Month to get
	 * @return ArrayList of ActivityModels for that day.
	 */
	public ArrayList<ActivityModel> getScheduleMonth(Date month){
		Date ThisMonth = ct.setDayOfMonth(month, 1);
		ArrayList<ActivityModel> retur = new ArrayList<ActivityModel>();
		for (int i = 0; i < ct.getLastDayOfMonth(ThisMonth); i++) {
			retur.addAll(getScheduleDay(month));
			month = ct.getNextDay(month);
		}
		return retur;
	}
	
	/**
	 * A method for adding an activity from your calendar
	 * @param the Activity Model
	 * @return whether it worked
	 */
	public boolean addAppointment(ActivityModel AM){
		return activities.add(AM);
	}
	
	/**
	 * A method for removing an activity from your calendar
	 * @param the Activity Model
	 * @return whether it worked
	 */
	public boolean removeAppointment(ActivityModel AM){
		return activities.remove(AM);
	}
}
